{% extends 'layout.html' %}

{% block content %}
<div class="container">
    <div>
        <input type="button" value="新建订单1" class="btn btn-success" data-toggle="modal" data-target="#myModal" >
        <input  id="btnAdd" type="button" value="新建订单2" class="btn btn-success" >
    </div>

    <div class="panel panel-default" style="margin-top: 10px;">
                
        <div class="panel-heading">
            <span class="glyphicon glyphicon-list" aria-hidden="true"></span>
            订单
        </div>
  
        <!-- Table -->
        <table class="table table-bordered">
          <thead>
            <tr>
              <th>ID</th>
              <th>订单号</th>
              <th>商品名称</th>
              <th>价格</th>
              <th>状态</th>
              <th>管理员</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
            {% for obj in queryset %}
            <tr uid="{{ obj.id }}">
              <th scope="row">{{ obj.id }}</th>
              <td>{{ obj.oid }}</td>
              <td>{{ obj.title }}</td>
              <td>{{ obj.price }}</td>
              <td>{{ obj.get_status_display }}</td>
              <td>{{ obj.admin.username }}</td>
              <td>
                <input uid="{{ obj.id }}" type="button" class="btn btn-primary btn-xs btn-edit" value="编辑">
                <input uid="{{ obj.id }}" class="btn btn-danger btn-xs btn-delete" type="button" value="删除">
            </td>
            </tr>
            {% endfor %}
          </tbody>
        </table>
      </div>

      <ul class="pagination">
        {{ page_string }}
      </ul>
      <span><a style="color: red;">{{ error }}</a></span>

        <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">订单填写</h4>
        </div>
        <div class="modal-body">
            <form id="addForm" novalidate>
                <div class="clearfix" >
                    {% for field in form %}
                        <div class="col-xs-6" >
                            <div class="form-group" style="position: relative;margin-bottom: 20px;" > 
                                <label>{{ field.label }}</label>
                                    {{ field }}   
                                    <span class="error-msg" style="color: red; position: absolute;"></span> 
                            </div>
                        </div>
                    {% endfor %}  
                </div>
            </form>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
            <button  id="btnSave" type="button" class="btn btn-primary">保 存</button>
        </div>
        </div>
    </div>
    </div>

    <!-- 删除（对话框） -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">

            <div class="alert alert-danger alert-dismissible fade in" role="alert">
                <h4>是否确定删除？</h4>
                <p style="margin: 10px 0;"> 删除后，所有关联的相关数据都会被删除!</p>
                <p style="text-align: right;">
                    <button id="btnConfirmDelete" type="button" class="btn btn-danger">确 定</button>
                    <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                </p>
            </div>

        </div>
    </div>

</div>

{% endblock %}

{% block js %}

    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID;

        $(function () {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnConfirmDeleteEvent();
            bindBtnEditEvent();
        })

        function bindBtnAddEvent() {
            $("#btnAdd").click(function () {

                EDIT_ID = undefined;
                // 清空弹出框里面的内容
                $("#addForm")[0].reset();
                
                //编辑输入框的标题
                $("#myModalLabel").text("添加新订单");


                //点击显示对话框
                $('#myModal').modal('show');
                
            })
            
        }

        function bindBtnEditEvent() {
            $(".btn-edit").click(function () {
                // 清空弹出框里面的内容
                $("#addForm")[0].reset();

                // 点击后获取 id值
                var uid = $(this).attr("uid");
                EDIT_ID = uid;

                //编辑输入框的标题
                $("#myModalLabel").text("订单编辑");

                // 展示弹出框
                $('#myModal').modal('show');

                // 向后台发送ajax请求 获取数据
                $.ajax({
                    url:"/order/detail/",
                    type : "get",
                    data : {
                        uid : uid,
                    },
                    dataType : "JSON",
                    success:function (res) {
                        if (res.status) {
                            $.each(res.data,function (name,value) {
                                $("#id_"+name).val(value);
                            })
                        }else{
                            alert(res.error);
                        }
                    }
                })


            });
            
        }
        
        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function () {
                 //点击显示对话框
                $('#deleteModal').modal('show');

                //获取当前行的ID并赋值给全局变量
                DELETE_ID = $(this).attr("uid");
            })
           
        }

        function bindBtnConfirmDeleteEvent() {
            $("#btnConfirmDelete").click(function () {
                $.ajax({
                    url:"/order/delete/",
                    type:"GET",
                    data:{
                        uid:DELETE_ID,
                    },
                    dataType:"JSON",
                    success:function (res){
                        if (res.status) {
                            //隐藏删除框
                            //$("#deleteModal").modal('hide');
                            //在页面上删除当前一行数据（js格式删除）
                            //$("tr[uid="+ DELETE_ID +"]").remove();
                            //将其数据制空
                            //DELETE_ID = 0;
                            
                            //简单思路 可以直接刷新
                            location.reload();    

                        }else{
                            alert(res.error);

                        }
                    }
                })
            });
            
        }

        function bindBtnSaveEvent() {
            $("#btnSave").click(function () {
                //在点击之前清空 span的框的值
                $(".error-msg").empty();
                if (EDIT_ID) {
                    // 编辑
                    doEdit();
                }else{
                    // 添加
                    doAdd();
                }
            })
            
        }

        function doAdd() {
            $.ajax({
                    url:"/order/add/",
                    type:'post',
                    data : $("#addForm").serialize(),
                    dataType:'JSON',
                    success:function (res) {
                        if (res.status) {
                            //alert("创建成功");
                            
                            //清空表单  $("#addForm")是JQuery对象  $("#addForm")[0] 是DOM对象
                            $("#addForm")[0].reset();

                            //关闭对话框
                            $('#myModal').modal('hide');
                            location.reload();

                        }else{
                            $.each(res.error,function (name,errorList) {
                                $("#id_"+name).next().text(errorList[0]);
                            })
                        }
                        
                    }

                })
         
        }

        function doEdit() {
            $.ajax({
                    url:"/order/edit/"+"?uid="+EDIT_ID,
                    type:'post',
                    data : $("#addForm").serialize(),
                    dataType:'JSON',
                    success:function (res) {
                        if (res.status) {
                            //alert("创建成功");
                            
                            //清空表单  $("#addForm")是JQuery对象  $("#addForm")[0] 是DOM对象
                            $("#addForm")[0].reset();

                            //关闭对话框
                            $('#myModal').modal('hide');
                            location.reload();

                        }else{
                            if (res.tips) {
                                alert(res.tips);
                            }else{
                                $.each(res.error,function (name,errorList) {
                                    $("#id_"+name).next().text(errorList[0]);
                                })
                            }
                        }
                        
                    }

                })

        }


    </script>

{% endblock %}

